package com.member.web.controller.shop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.utils.poi.ExcelUtil;
import com.member.shop.domain.SysBizMaterial;
import com.member.shop.domain.SysBizMaterialCategory;
import com.member.shop.domain.SysBizProfileCategory;
import com.member.shop.service.ISysBizMaterialCategoryService;
import com.member.shop.service.ISysBizMaterialService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【素材分类】控制器
 *
 * @author lixinchen
 * @version 1.0
 * @date 2024-05-08
 **/
@RestController
@RequestMapping("/shop/materialCategory")
public class SysBizMaterialCategoryController extends BaseController {
    @Resource
    private ISysBizMaterialCategoryService sysBizMaterialCategoryService;

    @Resource
    private ISysBizMaterialService sysBizMaterialService;
    /**
     * 查询素材分类列表
     */
    @PreAuthorize("@ss.hasPermi('shop:materialCategory:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizMaterialCategory sysBizMaterialCategory) {
        startPage();
        LambdaQueryWrapper<SysBizMaterialCategory> queryWrapper = new LambdaQueryWrapper<SysBizMaterialCategory>();
        queryWrapper.orderByDesc(SysBizMaterialCategory::getCreateTime);
        List<SysBizMaterialCategory> list = sysBizMaterialCategoryService.selectSysBizMaterialCategoryList(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询素材分类列表
     */
    @PreAuthorize("@ss.hasPermi('shop:materialCategory:list')")
    @GetMapping("/list2")
    public TableDataInfo list2(SysBizMaterialCategory sysBizMaterialCategory) {
        LambdaQueryWrapper<SysBizMaterialCategory> queryWrapper = new LambdaQueryWrapper<SysBizMaterialCategory>();
        queryWrapper.orderByDesc(SysBizMaterialCategory::getCreateTime);
        List<SysBizMaterialCategory> list = sysBizMaterialCategoryService.selectSysBizMaterialCategoryList(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出素材分类列表
     */
    @PreAuthorize("@ss.hasPermi('shop:materialCategory:export')")
    @Log(title = "素材分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizMaterialCategory sysBizMaterialCategory) {
        LambdaQueryWrapper<SysBizMaterialCategory> queryWrapper = new LambdaQueryWrapper<SysBizMaterialCategory>();
        List<SysBizMaterialCategory> list = sysBizMaterialCategoryService.selectSysBizMaterialCategoryList(queryWrapper);
        ExcelUtil<SysBizMaterialCategory> util = new ExcelUtil<SysBizMaterialCategory>(SysBizMaterialCategory.class);
        util.exportExcel(response, list, "素材分类数据");
    }

    /**
     * 获取素材分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:materialCategory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysBizMaterialCategoryService.selectSysBizMaterialCategoryById(id));
    }

    /**
     * 新增素材分类
     */
    @PreAuthorize("@ss.hasPermi('shop:materialCategory:add')")
    @Log(title = "素材分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizMaterialCategory sysBizMaterialCategory) {
        LambdaQueryWrapper<SysBizMaterialCategory> sysBizMaterialCategoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysBizMaterialCategoryLambdaQueryWrapper.eq(SysBizMaterialCategory::getMaterialCategoryName,sysBizMaterialCategory.getMaterialCategoryName());
        if(!sysBizMaterialCategoryService.selectSysBizMaterialCategoryList(sysBizMaterialCategoryLambdaQueryWrapper).isEmpty()){
            return AjaxResult.warn("分类名称已存在");
        }
        return toAjax(sysBizMaterialCategoryService.insertSysBizMaterialCategory(sysBizMaterialCategory));
    }

    /**
     * 修改素材分类
     */
    @PreAuthorize("@ss.hasPermi('shop:materialCategory:edit')")
    @Log(title = "素材分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizMaterialCategory sysBizMaterialCategory) {
        LambdaQueryWrapper<SysBizMaterialCategory> sysBizMaterialCategoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysBizMaterialCategoryLambdaQueryWrapper.eq(SysBizMaterialCategory::getMaterialCategoryName,sysBizMaterialCategory.getMaterialCategoryName());
        sysBizMaterialCategoryLambdaQueryWrapper.ne(SysBizMaterialCategory::getId,sysBizMaterialCategory.getId());
        if(!sysBizMaterialCategoryService.selectSysBizMaterialCategoryList(sysBizMaterialCategoryLambdaQueryWrapper).isEmpty()){
            return AjaxResult.warn("分类名称已存在");
        }
        return toAjax(sysBizMaterialCategoryService.updateSysBizMaterialCategory(sysBizMaterialCategory));
    }

    /**
     * 删除素材分类
     */
    @PreAuthorize("@ss.hasPermi('shop:materialCategory:remove')")
    @Log(title = "素材分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            LambdaQueryWrapper<SysBizMaterial> sysBizMaterialLambdaQueryWrapper = new LambdaQueryWrapper<>();
            sysBizMaterialLambdaQueryWrapper.eq(SysBizMaterial::getCategoryId,id);
            List<SysBizMaterial> sysBizMaterials = sysBizMaterialService.selectSysBizMaterialList(sysBizMaterialLambdaQueryWrapper);
            if(!CollectionUtils.isEmpty(sysBizMaterials)){
                return AjaxResult.warn("该素材分类已分配,不允许删除");
            }
        }
        return toAjax(sysBizMaterialCategoryService.deleteSysBizMaterialCategoryByIds(ids));
    }
}
